package darknet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * The Darknet registry.
 * This class is useful to get some use informations.
 * 
 * @author Yoyo
 */
public class DarknetRegistry {

    /**
     * Write the configuration passed in parameters.
     *
     * @param workingDirectory
     * @param username
     * @param password
     * @param address
     * @param port
     */
    public static void writeConfig(String workingDirectory, String username,
            String password, String address,
            String port) {

        if (workingDirectory == null || workingDirectory.length() == 0) {
            throw new IllegalArgumentException("workingDirectory is invalid");
        }
	if (username == null || username.length() == 0) {
	    throw new IllegalArgumentException("username is invalid");
	}
	if (password == null || password.length() == 0) {
	    throw new IllegalArgumentException("password is invalid");
	}
	if (address == null || address.length() == 0) {
	    throw new IllegalArgumentException("address is invalid");
	}
	if (port == null || port.length() == 0) {
	    throw new IllegalArgumentException("prot is invalid");
	}

	try {

            // Create the working directory ?
            File workingDir = new File(workingDirectory);
            if (!workingDir.exists()) {
                workingDir.mkdirs();
            }

            File confFile = new File(workingDirectory + File.separator + "user.conf");

	    PrintWriter printer = new PrintWriter(
		    new FileOutputStream(confFile));

	    printer.println("Darknet v.0.1");
	    printer.println("username = " + username);
	    printer.println("password = " + password);
	    printer.println("address = " + address);
	    printer.println("port = " + port);

	    printer.flush();
	    printer.close();

	} catch (IOException e) {
            System.out.println(e);
	    throw new RuntimeException(e);
	}
    }
    
    /** The working directory */
    private File workingDirectory;
    
    /** The user name */
    private String username;
    
    /** The password */
    private String password;
    
    /** The address */
    private String address;
    
    /** The port */
    private Integer port;
    
    /**
     * Initialise a new Darket Registry.
     * 
     * In the working directory, it seek the "user.conf" file,
     * and start to read it.
     * If the working directory don't exist, the working directory and the
     * user.conf will be create.
     * @param workingDirectory 
     */
    public DarknetRegistry(String workingDirectory) {
        
        File workingDirectoryFile = new File(workingDirectory);

        File confFile = new File(workingDirectoryFile.getAbsolutePath()
                + File.separator + "user.conf");

        this.workingDirectory = workingDirectoryFile;
	
	if (!workingDirectoryFile.exists()) {
	    workingDirectoryFile.mkdirs();
	}
	
	try {
	    
	    BufferedReader reader = new BufferedReader(
                    new FileReader(confFile));
            
	    reader.readLine();
	    username = reader.readLine().split("username = ")[1];
	    password = reader.readLine().split("password = ")[1];
	    address = reader.readLine().split("address = ")[1];
	    port = Integer.parseInt(reader.readLine().split("port = ")[1]);
	    
	    reader.close();
	    
	} catch (FileNotFoundException e) {
	    throw new RuntimeException(e);
	} catch (IOException e) {
	    throw new RuntimeException(e);
	}
	
    }

    /**
     * Get a string representation to the working directory.
     * @return
     */
    public File getWorkingDirectory() {
	return workingDirectory;
    }

    /**
     * Get the user name.
     * @return
     */
    public String getUsername() {
	return username;
    }

    /**
     * Get the user password.
     * @return
     */
    public String getPassword() {
	return password;
    }

    /**
     * Get the string representation of a InetAddress.
     * @return
     */
    public String getAddress() {
	return address;
    }

    /**
     * Get the port to listen.
     * @return
     */
    public Integer getPort() {
	return port;
    }
}
